package com.tianji.jersey.resource;




import java.io.IOException;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.apache.commons.fileupload.FileUploadException;

@Path("/jersey")
public class ImageUploadResource {
	private Hashtable<String, String> data = new Hashtable<String, String>();

	/** Creates a new instance of MapResource 
	 * @throws FileUploadException */
	 @Context
	 HttpServletRequest servletRequest;

	@POST
	@Path("media")
	@Produces("text/plain")
	public String getMedia(String value) throws IOException, FileUploadException {		
		
		return "Hello World----------->"+value;
	}

	@GET
	@Path("media")
	@Produces("text/plain")
	public String getMedia1() throws IOException {
		System.out.println("=====get=============>"+servletRequest.getParameter("id"));
		
		return "Hello World";
	}
	@GET
	@Path("media2")
	@Produces({"application/xml","application/json"})
	public Object getMedia2() throws IOException {
		System.out.println("=====get=============>"+servletRequest.getParameter("id"));
		AnswerVO vo = new AnswerVO();
		vo.setAnswerId(1);
		vo.setAnswerTitle("测试1");
		AnswerVoList list = new AnswerVoList();
		list.getAnswerVoList().add(vo);
		return list;
	}
	@GET
	@Path("media4")
	@Produces({"application/xml","application/json"})
	public Object getMedia4() throws IOException {
		System.out.println("=====get=============>"+servletRequest.getParameter("id"));
		String name = "aaaaaaaaaaaaaaaaaa";
		return name;
	}
	@GET
	@Path("media3")
	@Produces("application/json")
	public String getMedia3() throws IOException {
		System.out.println("=====get=============>"+servletRequest.getParameter("id"));
		
		return "Hello Json";
	}


	@POST
	@Path("media2")
	@Produces("application/xml")
	public Hashtable<String, String> updateDataItem(NameValuePair item) {
		System.out.println("======updateDataItem========"+item.getName());
		if (item.getName() != null) {
			if (item.getValue() == null || item.getValue().length() == 0)
				data.remove(item.getName());
			else
				data.put(item.getName(), item.getValue());
		}
		return data;
	}
	@POST
	@Path("data")
	@Produces("text/html")
	public Hashtable<String, String> getData(
			@FormParam("name") String name,
			@FormParam("value") String value) {
		System.out.println("======getData========");
		Hashtable<String,String> table = new Hashtable<String,String>();
		table.put("name", name);
		table.put("value", value);
		return table;
	}
	@GET
	@Path("jsonp/callback={callback}/")
	@Produces("text/plain")
	public String getJsonp(@PathParam("callback") String callback) throws IOException {
		System.out.println("=====contentType=============>"+servletRequest.getContentType());
		
//		String callback = servletRequest.getParameter("callback");
		System.out.println("=====callback111=============>"+callback);
		String result = callback+"({name:'kang'})";
		System.out.println("=====result=============>"+result);
		return result;
	}
}
